/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.invoice.controller;
import com.invoice.busines.ArticuloBO;
import com.invoice.busines.ArticuloBOImpl;
import com.invoice.busines.dto.ArticuloDTO;
import com.invoice.busines.dto.ArticulosInventarioDTO;
import com.invoice.busines.dto.ComprobanteDetalleDTO;
import com.invoice.busines.dto.ArticuloCampoDTO;
import com.invoice.enums.Estado;
import com.invoice.service.contadores.Contador;
import com.invoice.service.contadores.ContadorArticulos;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author laura
 */
@ManagedBean
@SessionScoped
public class ArticuloBean{
    private static final Logger logger = Logger.getLogger(ArticuloBean.class.getName());
    private final ArticuloBO articuloBO = new ArticuloBOImpl();
    private ArticuloDTO articuloDTO = new ArticuloDTO();
    private final Contador contador = new ContadorArticulos();
    private List<ArticuloDTO> articulos = new ArrayList<ArticuloDTO>();
    private List<ArticuloCampoDTO> labels = new ArrayList<ArticuloCampoDTO>();
    private boolean habEdicion = false;
    
    @PostConstruct
    public void init() {
        try {
            articulos = articuloBO.findAll();
        } catch (Exception ex) {
            Logger.getLogger(ArticuloBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public ArticuloDTO getArticuloDTO() {
        return articuloDTO;
    }    
    public void setArticuloDTO(ArticuloDTO articuloDTO) {
        this.articuloDTO = articuloDTO;
    }    
    public boolean getHabEdicion(){
        return habEdicion;
    }
    public List<ArticuloCampoDTO> getLabels(){
        return labels;
    }
    public List<ArticuloDTO> articulos() {
        try {
            articulos =  articuloBO.findAll();
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return articulos;
    }
    
    public String addArticulo(){
        habEdicion = false;
        articuloDTO = new ArticuloDTO();
        articuloDTO.setCode(contador.generarClave(""));
        articuloDTO.setStock(BigDecimal.ZERO);
        labels = new ArrayList<ArticuloCampoDTO>();
        return "RegistroProducto";
    }
    
    public String saveArticulo(){
        if(labels != null){
            articuloDTO.setArticuloCampoDTOs(labels);
        }
        try {
            if(habEdicion){
                articuloBO.updateArticulo(articuloDTO);
            } else {
                articuloDTO = articuloBO.addArticulo(articuloDTO);
            }
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        habEdicion = false;
        return "ListaProductos";
    }
    
    public String editArticulo(){
        habEdicion = true;
        labels = articuloDTO.getArticuloCampoDTOs();
        if(labels == null){
            labels = new ArrayList<ArticuloCampoDTO>();
        }
        return "RegistroProducto";
    }
    
    public void disabledArticulo(){
        try {
            articuloDTO.setEstado(Estado.Anulado);
            articuloBO.updateArticulo(articuloDTO);
            addMessage("Mensaje", "Articulo Borrado.");
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }
    
    public void addArticuloCampo() {
        ArticuloCampoDTO articuloCampoDTO = new ArticuloCampoDTO();
        labels.add(articuloCampoDTO);       
    }
    
    public List<ArticuloDTO> completeArticulo(String query) {
        query = query.toLowerCase();
        List<ArticuloDTO> filteredThemes = new ArrayList<ArticuloDTO>();
        try {
            List<ArticuloDTO> allThemes = articuloBO.findByHabilitado();
            for (ArticuloDTO skin : allThemes) {
                if(skin.getDescripcion().toLowerCase().startsWith(query) || skin.getCode().toLowerCase().startsWith(query)) {
                    filteredThemes.add(skin);
                }
            }
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return filteredThemes;
    }
    
    public BigDecimal cantidadComprobante(){
        BigDecimal result = BigDecimal.ZERO;
        if(articuloDTO.getComprobanteDetalleDTO() != null){
            for (ComprobanteDetalleDTO comprobanteDetalleDTO : articuloDTO.getComprobanteDetalleDTO()) {
                if(comprobanteDetalleDTO.getComprobanteDTO().getEstado().equals(Estado.Procedio)){
                    result.add(comprobanteDetalleDTO.getCant());
                }
            }
        }
        return result;
    }
    
    public BigDecimal cantidadInventario(){
        BigDecimal result = BigDecimal.ZERO;
        if(articuloDTO.getArticulosInventario() != null){
            for (ArticulosInventarioDTO  articulosInventarioDTO: articuloDTO.getArticulosInventario()) {
                result.add(articulosInventarioDTO.getCantidad());
            }
        }
        return result;
    }
    
    public void addMessage(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}